Completed
Push — master ( 12029f...68e9ab )
by Maxence
02:26
created

Circles.initialize   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
/*
2
 * Circles - Bring cloud-users closer together.
3
 *
4
 * This file is licensed under the Affero General Public License version 3 or
5
 * later. See the COPYING file.
6
 *
7
 * @author Maxence Lange <[email protected]>
8
 * @copyright 2017
9
 * @license GNU AGPL version 3 or any later version
10
 *
11
 * This program is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU Affero General Public License as
13
 * published by the Free Software Foundation, either version 3 of the
14
 * License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU Affero General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License
22
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23
 *
24
 */
25
26
/** global: OC */
27
/** global: OCA */
28
29
(function () {
30
31
32
	/**
33
	 * @constructs Circles
34
	 */
35
	var Circles = function () {
36
		this.initialize();
37
	};
38
39
	Circles.prototype = {
40
41
42
		initialize: function () {
43
44
			var self = this;
45
46
47
			/**
48
			 * API function to create a new Circle.
49
			 *
50
			 * @param type
51
			 * @param name
52
			 * @param callback
53
			 */
54
			this.createCircle = function (type, name, callback) {
55
56
				var result = {status: -1};
57
				$.ajax({
58
					method: 'PUT',
59
					url: OC.generateUrl('/apps/circles/v1/circles'),
60
					data: {
61
						type: type,
62
						name: name
63
					}
64
				}).done(function (res) {
65
					self.onCallback(callback, res);
66
				}).fail(function () {
67
					self.onCallback(callback, result);
68
				});
69
			};
70
71
72
			this.listCircles = function (type, name, level, callback) {
73
				var result = {status: -1};
74
				$.ajax({
75
					method: 'GET',
76
					url: OC.generateUrl('/apps/circles/v1/circles'),
77
					data: {
78
						type: type,
79
						name: name,
80
						level: level
81
					}
82
				}).done(function (res) {
83
					self.onCallback(callback, res);
84
				}).fail(function () {
85
					self.onCallback(callback, result);
86
				});
87
			};
88
89
90
			this.detailsCircle = function (circleId, callback) {
91
				var result = {status: -1};
92
				$.ajax({
93
					method: 'GET',
94
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId)
95
				}).done(function (res) {
96
					self.onCallback(callback, res);
97
				}).fail(function () {
98
					self.onCallback(callback, result);
99
				});
100
			};
101
102
103
			this.addMember = function (circleId, userId, callback) {
104
				var result = {status: -1};
105
				$.ajax({
106
					method: 'PUT',
107
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/members'),
108
					data: {
109
						name: userId
110
					}
111
				}).done(function (res) {
112
					self.onCallback(callback, res);
113
				}).fail(function () {
114
					self.onCallback(callback, result);
115
				});
116
			};
117
118
119
			this.addGroupMembers = function (circleId, groupId, callback) {
120
				var result = {status: -1};
121
				$.ajax({
122
					method: 'PUT',
123
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/group'),
124
					data: {
125
						name: groupId
126
					}
127
				}).done(function (res) {
128
					self.onCallback(callback, res);
129
				}).fail(function () {
130
					self.onCallback(callback, result);
131
				});
132
			};
133
134
135
			this.removeMember = function (circleId, userId, callback) {
136
				var result = {status: -1};
137
				$.ajax({
138
					method: 'DELETE',
139
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/members'),
140
					data: {
141
						member: userId
142
					}
143
				}).done(function (res) {
144
					self.onCallback(callback, res);
145
				}).fail(function () {
146
					self.onCallback(callback, result);
147
				});
148
			};
149
150
151
			this.levelMember = function (circleId, member, level, callback) {
152
				var result = {status: -1};
153
				$.ajax({
154
					method: 'POST',
155
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/level'),
156
					data: {
157
						member: member,
158
						level: level
159
					}
160
				}).done(function (res) {
161
					self.onCallback(callback, res);
162
				}).fail(function () {
163
					self.onCallback(callback, result);
164
				});
165
			};
166
167
168
			this.joinCircle = function (circleId, callback) {
169
				var result = {status: -1};
170
				$.ajax({
171
					method: 'GET',
172
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/join'),
173
					data: {}
174
				}).done(function (res) {
175
					self.onCallback(callback, res);
176
				}).fail(function () {
177
					self.onCallback(callback, result);
178
				});
179
			};
180
181
182
			this.settingsCircle = function (circleId, settings, callback) {
183
				var result = {status: -1};
184
				$.ajax({
185
					method: 'POST',
186
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/settings'),
187
					data: {settings: settings}
188
				}).done(function (res) {
189
					self.onCallback(callback, res);
190
				}).fail(function () {
191
					self.onCallback(callback, result);
192
				});
193
			};
194
195
196
			this.leaveCircle = function (circleId, callback) {
197
				var result = {status: -1};
198
				$.ajax({
199
					method: 'GET',
200
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/leave'),
201
					data: {}
202
				}).done(function (res) {
203
					self.onCallback(callback, res);
204
				}).fail(function () {
205
					self.onCallback(callback, result);
206
				});
207
			};
208
209
210
			this.destroyCircle = function (circleId, callback) {
211
				var result = {status: -1};
212
				$.ajax({
213
					method: 'DELETE',
214
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId),
215
					data: {}
216
				}).done(function (res) {
217
					self.onCallback(callback, res);
218
				}).fail(function () {
219
					self.onCallback(callback, result);
220
				});
221
			};
222
223
224
			this.shareToCircle = function (circleId, source, type, item, callback) {
225
				var result = {status: -1};
226
				$.ajax({
227
					method: 'PUT',
228
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/share'),
229
					data: {
230
						source: source,
231
						type: type,
232
						item: item
233
					}
234
				}).done(function (res) {
235
					self.onCallback(callback, res);
236
				}).fail(function () {
237
					self.onCallback(callback, result);
238
				});
239
			};
240
241
242
			this.linkCircle = function (circleId, remote, callback) {
243
				var result = {status: -1};
244
				$.ajax({
245
					method: 'POST',
246
					url: OC.generateUrl('/apps/circles/v1/circles/' + circleId + '/link'),
247
					data: {
248
						remote: remote
249
					}
250
				}).done(function (res) {
251
					self.onCallback(callback, res);
252
				}).fail(function () {
253
					self.onCallback(callback, result);
254
				});
255
			};
256
257
258
			this.linkStatus = function(linkId, status, callback) {
259
				var result = {status: -1};
260
				$.ajax({
261
					method: 'POST',
262
					url: OC.generateUrl('/apps/circles/v1/link/' + linkId + '/status'),
263
					data: {
264
						status: status
265
					}
266
				}).done(function (res) {
267
					self.onCallback(callback, res);
268
				}).fail(function () {
269
					self.onCallback(callback, result);
270
				});
271
			};
272
273
			this.onCallback = function (callback, result) {
274
				if (callback && (typeof callback === 'function')) {
275
					if (typeof result === 'object') {
276
						callback(result);
277
					} else {
278
						callback({status: -1})
0 ignored issues
show
Coding Style introduced by
There should be a semicolon.

Requirement of semicolons purely is a coding style issue since JavaScript has specific rules about semicolons which are followed by all browsers.

Further Readings:

Loading history...
279
					}
280
				}
281
			};
282
283
		}
284
285
	};
286
287
	OCA.Circles = Circles;
288
	OCA.Circles.api = new Circles();
289
290
})();
291
292
293